﻿using Models.Mailbox;
using Newtonsoft.Json;
using SendMessagePage.Util;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SendMessagePage.HomePage
{
    /// <summary>
    /// SendExcelConfigure.xaml 的交互逻辑
    /// </summary>
    public partial class SendExcelConfigure : Window
    {
        Result Result { get; set; }

        //查询邮箱配置
        private string UpdateMailboxConfigUrl = ConfigurationManager.AppSettings["UpdateMailboxConfig"];

        public SendExcelConfigure(Result result):this()
        {
            Result = result;
            this.textFromEmail.Text = result.FromEmail;
            this.textSmtpPort.Text = result.SmtpPort.ToString();
            this.textSmtpServer.Text = result.SmtpServe;
            this.textExcelPassWord.Text = result.ExcelPassWord;
        }
            public SendExcelConfigure()
        {
            InitializeComponent();
            Closing += Window_Closing;
             
        }

     

        private async void Submit_Click(object sender, RoutedEventArgs e)
        {
            string fromEmail = this.textFromEmail.Text.Trim();
            string smtpPort = this.textSmtpPort.Text.Trim();
            string smtpServe = this.textSmtpServer.Text;
            string excelPassWord = this.textExcelPassWord.Text;

            //ckChecked = ckPwd.IsChecked.Value;

            if (fromEmail.Length == 0)
            {
                MessageBox.Show("邮箱不能为空", "登录提示");
                this.textFromEmail.Focus();
                return;
            }

            if (smtpPort.Length == 0)
            {
                MessageBox.Show("SMTP端口不能为空", "登录提示");
                this.textSmtpPort.Focus();
                return;
            }

            if (smtpServe.Length == 0)
            {
                MessageBox.Show("SMTP服务器不能为空", "登录提示");
                this.textSmtpServer.Focus();
                return;
            }

            if (excelPassWord.Length == 0)
            {
                MessageBox.Show("授权码不能为空", "登录提示");
                this.textExcelPassWord.Focus();
                return;
            }

            int port = Convert.ToInt32(smtpPort);

            if (!Result.FromEmail.Equals(fromEmail) || !Result.SmtpPort.Equals(port) || !Result.SmtpServe.Equals(smtpServe) || !Result.ExcelPassWord.Equals(excelPassWord)) {
                MailboxConfigure configure = new MailboxConfigure(0, fromEmail, Convert.ToInt32(smtpPort), smtpServe, excelPassWord, PackingUserLogin.User.UserName);

                 string json = JsonConvert.SerializeObject(configure);
                 string result = await AccessWebApiUtil.PostAsyncJson(UpdateMailboxConfigUrl, json, PackingUserLogin.token);
                MessageBox.Show("!");
            }


            MessageBox.Show("提交成功!");

        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MainWindow mainWindow = new MainWindow();
            if (MessageBox.Show("是否要关闭？", "确认", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                e.Cancel = false;
                mainWindow.Show();
                

            }
            else
            {
                e.Cancel = true;
            }



        }





        }
}
